package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.controller.form.BrandForm;
import com.sevenGroup.pawnshop.controller.form.SelectBrandForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.controller.info.BrandInfo;
import com.sevenGroup.pawnshop.dao.BrandFirstTypeDao;
import com.sevenGroup.pawnshop.dao.FirstTypeDao;
import com.sevenGroup.pawnshop.dao.GoodsDao;
import com.sevenGroup.pawnshop.exception.GlobalExpection;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Brand;
import com.sevenGroup.pawnshop.dao.BrandDao;
import com.sevenGroup.pawnshop.model.BrandFirstType;
import com.sevenGroup.pawnshop.model.FirstType;
import com.sevenGroup.pawnshop.model.Goods;
import com.sevenGroup.pawnshop.service.BrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandDao, Brand> implements BrandService {

    @Resource
    private BrandDao brandDao;

    @Resource
    private BrandFirstTypeDao brandFirstTypeDao;

    @Resource
    private FirstTypeDao firstTypeDao;

    @Resource
    private GoodsDao goodsDao;

    @Override
    public void saveBrand(BrandForm brandForm) throws sevenGroupException {

        if (brandForm == null) {

            throw new sevenGroupException(40001, "参数为空");

        }
        //新增品牌
        Brand brand_name = brandDao.selectOne(new QueryWrapper<Brand>().eq("brand_name", brandForm.getBrandName()));
        if (brand_name != null&&brand_name.getBrandId()!=brandForm.getBrandId()) {
            throw new sevenGroupException(40020, "该品牌已存在");
        }

        Brand brand = CopyProperties.copyOne(brandForm, Brand.class);
        brandDao.insert(brand);
        //获取新增的品牌id
        Integer brandId = brand.getBrandId();
        //如何集合为空，直接结束
        if (brandForm.getList() == null || brandForm.getList().size() <= 0) {
            return;
        }
        //循环传入的集合，并添加到中间表
        for (Integer integer : brandForm.getList()) {
            BrandFirstType brandFirstType = BrandFirstType.builder().brandId(brandId).firstId(integer).build();
            brandFirstTypeDao.insert(brandFirstType);
        }
    }

    @Override
    public IPage<BrandInfo> selectAllBrand(pageForm pageForm) throws sevenGroupException {
        if (pageForm == null) {
            throw new sevenGroupException(40001, "参数为空");
        }
        //创建分页对象集合
        pageForm.setPageNum(pageForm.getPageNum() == null ? 1 : pageForm.getPageNum());
        pageForm.setPageSize(pageForm.getPageSize() == null ? 5 : pageForm.getPageSize());
        Page brandPage = new Page<Brand>(pageForm.getPageNum(), pageForm.getPageSize());
        //分页查询
        Page page = brandDao.selectPage(brandPage, null);
        if (page == null) {
            throw new sevenGroupException(40001, "查询失败");
        }

        return page;
    }

    @Override
    public IPage<BrandInfo> selectBrandByBrandName(SelectBrandForm selectBrandForm) throws sevenGroupException {

        selectBrandForm.setPageNum(selectBrandForm.getPageNum() == null ? 1 : selectBrandForm.getPageNum());
        selectBrandForm.setPageSize(selectBrandForm.getPageSize() == null ? 5 : selectBrandForm.getPageSize());
        Page brandPage = new Page<Brand>(selectBrandForm.getPageNum(), selectBrandForm.getPageSize());

        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        //如果集合不为空，则进行嵌套查询
        if (selectBrandForm.getBrandName() != null && selectBrandForm.getBrandName() != "") {
            wrapper.and(wrapper1 -> wrapper1.like("brand_name", selectBrandForm.getBrandName()));
        }
        //设置查询条件
        if (selectBrandForm.getBrandState() != null && selectBrandForm.getBrandState() != "") {
            wrapper.eq("brand_state", selectBrandForm.getBrandState());
        }
        //分页查询
        Page page = brandDao.selectPage(brandPage, wrapper);

        if (page == null) {

            throw new sevenGroupException(40001, "查询失败");

        }

        return page;
    }

    @Override
    public List<BrandInfo> selectBrandByFirstTypeId(Integer firstTypeId) throws sevenGroupException {
        if (firstTypeId == null||firstTypeId<=0) {

            throw new sevenGroupException(40003, "参数异常");

        }

        List<BrandFirstType> first_id = brandFirstTypeDao.selectList(new QueryWrapper<BrandFirstType>().eq("first_id", firstTypeId));

        if (first_id == null) {
            throw new sevenGroupException(40004, "查询失败");
        }
        //创建集合，将所有的大类id存入
        ArrayList<Integer> integers = new ArrayList<>();
        for (BrandFirstType brandFirstType : first_id) {
            integers.add(brandFirstType.getBrandId());
        }
        if (integers == null || integers.size() <= 0) {
            return null;
        }
        //如果集合不为空，在进行后续的查询
        List<Brand> brand_id = brandDao.selectList(new QueryWrapper<Brand>().in("brand_id", integers).eq("brand_state","显示"));
        if (brand_id == null) {
            throw new sevenGroupException(40005, "查询失败");
        }

        List<BrandInfo> brandInfos = CopyProperties.copyList(brand_id, BrandInfo.class);
        return brandInfos;
    }

    @Override
    public BrandInfo selectBrandByBrandId(Integer brandId) throws sevenGroupException {
        if (brandId == null||brandId<=0) {

            throw new sevenGroupException(40003, "参数异常");

        }

        Brand brand = brandDao.selectById(brandId);

        QueryWrapper<BrandFirstType> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        //查询品牌和大类对应的
        List<BrandFirstType> brandFirstTypes = brandFirstTypeDao.selectList(wrapper);
        //新建集合将大类id存入
        ArrayList<Integer> integers = new ArrayList<>();

        for (BrandFirstType brandFirstType : brandFirstTypes) {
            integers.add(brandFirstType.getFirstId());
        }

        if (integers == null || integers.size() <= 0) {
            return CopyProperties.copyOne(brand, BrandInfo.class);
        }
        //集合不为空，在进行查询，新创建集合，将大类id存入后查询
        QueryWrapper<FirstType> wrapper1 = new QueryWrapper<>();
        wrapper1.in("first_type_id", integers);
        List<FirstType> firstTypes = firstTypeDao.selectList(wrapper1);
        ArrayList<Integer> integers1 = new ArrayList<>();
        for (FirstType firstType : firstTypes) {
            integers1.add(firstType.getFirstTypeId());
        }

        BrandInfo brandInfo = CopyProperties.copyOne(brand, BrandInfo.class);
        brandInfo.setList(integers1);
        return brandInfo;
    }

    @Override
    public void updateBrand(BrandForm brandForm) throws sevenGroupException {
        if (brandForm == null) {
            throw new sevenGroupException(40001, "参数为空");
        }
        //更新品牌不仅更新品牌表，还要更新商品表和商品大类中间表
        Brand brand_name = brandDao.selectOne(new QueryWrapper<Brand>().eq("brand_name", brandForm.getBrandName()));
        if (brand_name != null&&brand_name.getBrandId()!=brandForm.getBrandId()) {
            throw new sevenGroupException(40020, "该品牌已存在");
        }
        Brand brand = CopyProperties.copyOne(brandForm, Brand.class);
        brandDao.updateById(brand);

        //更新商品大类中间表
        QueryWrapper<BrandFirstType> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandForm.getBrandId());
        brandFirstTypeDao.delete(wrapper);
        for (Integer integer : brandForm.getList()) {
            BrandFirstType build = BrandFirstType.builder().firstId(integer).brandId(brandForm.getBrandId()).build();
            brandFirstTypeDao.insert(build);
        }
        //更新商品表
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("brand_id", brandForm.getBrandId());
        Goods goods = new Goods();
        goods.setBrandName(brandForm.getBrandName());
        goodsDao.update(goods, goodsQueryWrapper);

    }

}
